//
//  Copyright (c) 2022 Open Whisper Systems. All rights reserved.
//

#import "OWSOutgoingPaymentMessage.h"
#import <SignalCoreKit/NSDate+OWS.h>
#import <SignalServiceKit/SignalServiceKit-Swift.h>

NS_ASSUME_NONNULL_BEGIN

@interface OWSOutgoingPaymentMessage ()

@end

#pragma mark -

@implementation OWSOutgoingPaymentMessage

- (instancetype)initWithThread:(TSThread *)thread
           paymentCancellation:(nullable TSPaymentCancellation *)paymentCancellation
           paymentNotification:(nullable TSPaymentNotification *)paymentNotification
                paymentRequest:(nullable TSPaymentRequest *)paymentRequest
              expiresInSeconds:(uint32_t)expiresInSeconds
{
    OWSAssertDebug(paymentCancellation != nil || paymentNotification != nil || paymentRequest != nil);

    TSOutgoingMessageBuilder *messageBuilder = [TSOutgoingMessageBuilder outgoingMessageBuilderWithThread:thread];
    messageBuilder.expiresInSeconds = expiresInSeconds;
    self = [super initOutgoingMessageWithBuilder:messageBuilder];
    if (!self) {
        return self;
    }

    _paymentCancellation = paymentCancellation;
    _paymentNotification = paymentNotification;
    _paymentRequest = paymentRequest;

    return self;
}

// --- CODE GENERATION MARKER

// This snippet is generated by /Scripts/sds_codegen/sds_generate.py. Do not manually edit it, instead run `sds_codegen.sh`.

// clang-format off

- (instancetype)initWithGrdbId:(int64_t)grdbId
                      uniqueId:(NSString *)uniqueId
             receivedAtTimestamp:(uint64_t)receivedAtTimestamp
                          sortId:(uint64_t)sortId
                       timestamp:(uint64_t)timestamp
                  uniqueThreadId:(NSString *)uniqueThreadId
                   attachmentIds:(NSArray<NSString *> *)attachmentIds
                            body:(nullable NSString *)body
                      bodyRanges:(nullable MessageBodyRanges *)bodyRanges
                    contactShare:(nullable OWSContact *)contactShare
                 expireStartedAt:(uint64_t)expireStartedAt
                       expiresAt:(uint64_t)expiresAt
                expiresInSeconds:(unsigned int)expiresInSeconds
               isGroupStoryReply:(BOOL)isGroupStoryReply
              isViewOnceComplete:(BOOL)isViewOnceComplete
               isViewOnceMessage:(BOOL)isViewOnceMessage
                     linkPreview:(nullable OWSLinkPreview *)linkPreview
                  messageSticker:(nullable MessageSticker *)messageSticker
                   quotedMessage:(nullable TSQuotedMessage *)quotedMessage
    storedShouldStartExpireTimer:(BOOL)storedShouldStartExpireTimer
           storyAuthorUuidString:(nullable NSString *)storyAuthorUuidString
              storyReactionEmoji:(nullable NSString *)storyReactionEmoji
                  storyTimestamp:(nullable NSNumber *)storyTimestamp
              wasRemotelyDeleted:(BOOL)wasRemotelyDeleted
                   customMessage:(nullable NSString *)customMessage
                groupMetaMessage:(TSGroupMetaMessage)groupMetaMessage
           hasLegacyMessageState:(BOOL)hasLegacyMessageState
             hasSyncedTranscript:(BOOL)hasSyncedTranscript
              isFromLinkedDevice:(BOOL)isFromLinkedDevice
                  isVoiceMessage:(BOOL)isVoiceMessage
              legacyMessageState:(TSOutgoingMessageState)legacyMessageState
              legacyWasDelivered:(BOOL)legacyWasDelivered
           mostRecentFailureText:(nullable NSString *)mostRecentFailureText
          recipientAddressStates:(nullable NSDictionary<SignalServiceAddress *,TSOutgoingMessageRecipientState *> *)recipientAddressStates
              storedMessageState:(TSOutgoingMessageState)storedMessageState
             paymentCancellation:(nullable TSPaymentCancellation *)paymentCancellation
             paymentNotification:(nullable TSPaymentNotification *)paymentNotification
                  paymentRequest:(nullable TSPaymentRequest *)paymentRequest
{
    self = [super initWithGrdbId:grdbId
                        uniqueId:uniqueId
               receivedAtTimestamp:receivedAtTimestamp
                            sortId:sortId
                         timestamp:timestamp
                    uniqueThreadId:uniqueThreadId
                     attachmentIds:attachmentIds
                              body:body
                        bodyRanges:bodyRanges
                      contactShare:contactShare
                   expireStartedAt:expireStartedAt
                         expiresAt:expiresAt
                  expiresInSeconds:expiresInSeconds
                 isGroupStoryReply:isGroupStoryReply
                isViewOnceComplete:isViewOnceComplete
                 isViewOnceMessage:isViewOnceMessage
                       linkPreview:linkPreview
                    messageSticker:messageSticker
                     quotedMessage:quotedMessage
      storedShouldStartExpireTimer:storedShouldStartExpireTimer
             storyAuthorUuidString:storyAuthorUuidString
                storyReactionEmoji:storyReactionEmoji
                    storyTimestamp:storyTimestamp
                wasRemotelyDeleted:wasRemotelyDeleted
                     customMessage:customMessage
                  groupMetaMessage:groupMetaMessage
             hasLegacyMessageState:hasLegacyMessageState
               hasSyncedTranscript:hasSyncedTranscript
                isFromLinkedDevice:isFromLinkedDevice
                    isVoiceMessage:isVoiceMessage
                legacyMessageState:legacyMessageState
                legacyWasDelivered:legacyWasDelivered
             mostRecentFailureText:mostRecentFailureText
            recipientAddressStates:recipientAddressStates
                storedMessageState:storedMessageState];

    if (!self) {
        return self;
    }

    _paymentCancellation = paymentCancellation;
    _paymentNotification = paymentNotification;
    _paymentRequest = paymentRequest;

    return self;
}

// clang-format on

// --- CODE GENERATION MARKER

- (BOOL)shouldBeSaved
{
    return NO;
}

- (nullable SSKProtoDataMessageBuilder *)dataMessageBuilderWithThread:(TSThread *)thread
                                                          transaction:(SDSAnyReadTransaction *)transaction
{
    if (self.paymentCancellation == nil && self.paymentNotification == nil && self.paymentRequest == nil) {
        OWSFailDebug(@"Missing payload.");
        return nil;
    }

    SSKProtoDataMessageBuilder *builder = [super dataMessageBuilderWithThread:thread transaction:transaction];
    [builder setTimestamp:self.timestamp];

    if (self.paymentRequest != nil) {
        NSError *error;
        BOOL success = [self.paymentRequest addToDataBuilder:builder error:&error];
        if (error || !success) {
            OWSFailDebug(@"Could not build paymentRequest proto: %@.", error);
        }
    } else if (self.paymentNotification != nil) {
        NSError *error;
        BOOL success = [self.paymentNotification addToDataBuilder:builder error:&error];
        if (error || !success) {
            OWSFailDebug(@"Could not build paymentNotification proto: %@.", error);
        }
    } else if (self.paymentCancellation != nil) {
        NSError *error;
        BOOL success = [self.paymentCancellation addToDataBuilder:builder error:&error];
        if (error || !success) {
            OWSFailDebug(@"Could not build paymentCancellation proto: %@.", error);
        }
    }

    [builder setExpireTimer:self.expiresInSeconds];

    [builder setRequiredProtocolVersion:(uint32_t)SSKProtoDataMessageProtocolVersionPayments];
    return builder;
}

@end

NS_ASSUME_NONNULL_END
